/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package meteocal.business.boundary;

import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import meteocal.business.entity.Event;
import meteocal.business.entity.Notification;

/**
 *
 * @author Francesca
 */
@Stateless
public class NotificationFacade extends AbstractFacade<Notification>{
@PersistenceContext(unitName = "meteocalPU")
    private EntityManager em;

  
    
    public void create(Event idEvent, String user, Integer type){
        Notification notification = new Notification();
        notification.setIdEvent(idEvent);
        notification.setReceiver(user);
        notification.setType(type);
        if(findAlreadyExist(idEvent,user,type).isEmpty()){
           em.merge(notification);
           em.flush(); 
        }
        
        
    }
    
    public void createRescheduling(Event idEvent, String user, Integer type, Date proposed){
        Notification notification = new Notification();
        notification.setIdEvent(idEvent);
        notification.setReceiver(user);
        notification.setType(type);
        notification.setReschedulingDate(proposed);
        if(findAlreadyExist(idEvent, user, type).isEmpty()){
           em.persist(notification); 
           em.flush();
        }
        
        
        
    }
    
    public List<Notification> findAlreadyExist(Event idEvent, String user, Integer type){
        return em.createNamedQuery(Notification.FIND_ALREADY_EXIST)
                                   .setParameter("idEvent", idEvent)
                                   .setParameter("receiver", user)
                                   .setParameter("type", type)
                                   .getResultList();
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
    public NotificationFacade() {
        super(Notification.class);
    }
    
    public List<Notification> checkInvitation(String receiver){
        return em.createNamedQuery(Notification.FIND_INVITATION_NOTIFICATION, Notification.class).setParameter("receiver",receiver).getResultList();
    }
    
    public List<Notification> checkCancellation(String receiver){
        return em.createNamedQuery(Notification.FIND_CANCELLATION_NOTIFICATION, Notification.class).setParameter("receiver",receiver).getResultList();
    }
    
    public List<Notification> checkModification(String receiver){
        return em.createNamedQuery(Notification.FIND_MODIFICATION_NOTIFICATION, Notification.class).setParameter("receiver",receiver).getResultList();
    }
    
    public List<Notification> checkBadWeather(String receiver){
        return em.createNamedQuery(Notification.FIND_BAD_NOTIFICATION, Notification.class).setParameter("receiver",receiver).getResultList();
    }
    
    public List<Notification> checkRescheduling(String receiver){
        return em.createNamedQuery(Notification.FIND_RESCHEDULING_NOTIFICATION, Notification.class).setParameter("receiver",receiver).getResultList();
    }
    
    
    public Notification checkDetailsInvitation(Integer idNotification){
        return em.createNamedQuery(Notification.FIND_NOTIFICATION, Notification.class).setParameter("idNotification", idNotification).getSingleResult();
    }
    
    public void removeNotification(Integer idNotification){
        em.createNamedQuery(Notification.DELETE_NOTIFICATION, Notification.class).setParameter("idNotification", idNotification).executeUpdate();
    }
    
    public Notification checkIdNotification(Event idEvent,String pending,Integer type){
        return em.createNamedQuery(Notification.CHECK_NOTIFICATION, Notification.class)
                .setParameter("type",type)
                .setParameter("receiver",pending)
                .setParameter("idEvent",idEvent)
                .getSingleResult();
    }
    
    public void entityManager(){
        em.flush();
    }
    
    
}
